#include "WaypointGangster.h"
#include "Mob.h"
#include "Field.h"

bool WaypointGangster::action( QKeyEvent* ) {
    bool returnValue = false;
    #ifdef AIENABLED

    Mob* mob = getMob();
    int x = mob->getX(), y = mob->getY();
    int speed = mob->getMoveSpeed();

    if( waypointX == -1 || waypointY == -1 || (rand() % 100 == 77) ) {
        newWaypoint( this );
    }

    for( int i = 0; i < speed; i++ ) {
        if( evade() ) {
            returnValue = true;
            continue;
        }

        if( hasPath() ) {
            Direction c = cellToDirection( getPath(), mob );
            if( !(mob->getField()->move( c, 1, mob )) ) {
            clearPath();
            } else {
            returnValue = true;
            }
        } else {
            findPath( x, y, waypointX, waypointY );
        }

        Direction d = mob->getField()->enemyInSight( mob );
        if( d != NONE ) mob->attack( d );
    }

    #endif
    return returnValue;
}

void newWaypoint( WaypointGangster* w ) {
    static int waypoint1X = rand() % Field::WIDTH;
    static int waypoint1Y = rand() % Field::HEIGHT;

    w->setWaypointX( waypoint1Y );
    w->setWaypointY( waypoint1Y );
    if( rand() % 1000 == 77 ) {
        waypoint1X = rand() % Field::WIDTH;
        waypoint1Y = rand() % Field::HEIGHT;
    }
}
